
public class StartShopping
{
	ProductList myProducts;
	ProductList myCart;
	ProductList myHistory;
	Integer input;
	String myShop = "";
	
	public StartShopping()
	{
		myProducts = new ProductList("Products.xml");
		myCart = new ProductList("Cart.xml");
		myHistory = new ProductList("History.xml");
		input = 0;
	}
	
	void shopFromConsole()
	{
		System.out.println("Welcome to Dona Jurema");
		System.out.println();
		
		do
		{
			System.out.println("Currently shopping at: " + myShop);
			System.out.println("Choose an option:");
			System.out.println("1 - Show all products.");
			System.out.println("2 - Add new product.");
			System.out.println("3 - Remove a product.");
			System.out.println("4 - Show cart.");
			System.out.println("5 - Add to cart.");
			System.out.println("6 - Remove from cart.");
			System.out.println("7 - Buy item in cart.");
			System.out.println("8 - Change current shop.");
			System.out.println("9 - Exit.");
			
			input = Read.Integer();
			
			System.out.println("Your choice: " + input.toString());
			System.out.println();
			
			switch (input)
			{
				case 1 :
					myProducts.list();
					System.out.println();
					break;
				case 2 :
					this.addProductToDatabase();
					break;
				case 3 :
					this.removeProductFromDatabase();
					break;
				case 4 :
					myCart.list();
					System.out.println();
					break;
				case 5 :
					this.addItemToCart();
					break;
				case 6 :
					this.removeItemFromCart();
					break;
				case 7 :
					this.buyItemFromCart();
					break;
				case 8 :
					this.enterShopName();
					break;
			}
		}while (input < 9);
		
	}
	
	private void addProductToDatabase()
	{
		System.out.println("Product name: ");
		String name = Read.String();
		System.out.println();

		System.out.println(Units.getUnits());
		System.out.println("Unit: ");
		String unit = Read.String();
		System.out.println();
	
		Product p = new Product(name, unit);
		myProducts.Add(p);
	}
	
	private void removeProductFromDatabase()
	{
		System.out.println("Enter the name of the product to remove: ");
		String name = Read.String();
		Product p = myProducts.getProductByName(name);
		if (p != null)
		{
			myProducts.Remove(p);
		}
	}
	
	private void addItemToCart()
	{
		System.out.println("Product name: ");
		String name = Read.String();
		Product p = myProducts.getProductByName(name);
		if (p != null)
		{
			int quantity;
			do
			{
				System.out.println("Quantity (" + p.getUnit()+ "): ");
				quantity = Read.Integer();
				System.out.println();
				if (quantity == -1)
				{
					System.out.println("Numbers only!!");
				}
			} while (quantity == -1);
			p.setQuantity(quantity);	// Set the quantity.
			p.refreshDate();			// Set the date added to cart.
			myCart.Add(p);				// Add to cart.
		}
	}
	
	private void removeItemFromCart()
	{
		System.out.println("Product name: ");
		String name = Read.String();
		Product p = myCart.getProductByName(name);
		if (p != null)
		{
			myCart.Remove(p);
		}
	}
	
	private void buyItemFromCart()
	{
		// Check for Shop Name
		if (myShop.equals(""))
		{
			System.out.println("Shop name: ");
			myShop = Read.String();
			System.out.println();
		}
		
		System.out.println("Bought product name: ");
		String name = Read.String();
		Product p = myCart.getProductByName(name);
		if (p != null)
		{
			myCart.Remove(p);	// Remove product from cart
			p.setShop(myShop);	// Add the shop name
			p.refreshDate();	// Set the date of purchase
			myHistory.Add(p);	// Add to History and save.
		}
	}

	private void enterShopName()
	{
		System.out.println("Shop name: ");
		myShop = Read.String();
		System.out.println();
	}
}
